package com.xubeihong.xubeihong;

import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import javax.crypto.Cipher;

public class RSACryptographyExample {

    //改时间 id
    public static void main(String[] args) {
        String publicKeyStr = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwAlfODYPo54bq7OhYThqvMZItdXL0xBWSA5x02azvHjRZssmVLuX2daHd13MoUkdxsr5hcmgiBXh/gMegA8DDhBq14XIlmJFHQ5usHDp0ZY3vpqYQjxzkG+zzLzj4aInSsKt50jl5mpF5linVg2GpeAnszj786nVjf0fDf7vJZYZdsbTivuMLmUL708RHDUkrcC60yiRWtSqq/koSUxoSkiZgCYbhUuuy45+NN6G0BuCPMm1t9syv/e6ntkpxv1IKpcaViZF7/hGhhxErHzZBCIslgxYDc5/IxEc8mAn/G88IxSIpj+f5eIvFBdWeRQ3Rbz4Eb12h4/Z44Rr5ZfFXwIDAQAB";
        PublicKey publicKey = generatePublicKey(publicKeyStr);
        // 模拟要加密的数据对象
        DataObject dataObject = new DataObject(14825, "15675109504");
        LocalDateTime localDateTime = LocalDateTime.of(2024, 12, 18, 10, 0, 0);
        Timestamp timestamp = Timestamp.valueOf(localDateTime);
        long milliseconds = timestamp.getTime();
        dataObject.setTime(milliseconds / 1000);
        String jsonData = convertToJson(dataObject);
        List<String> encryptedData = encryptData(jsonData, publicKey);
        System.out.println("加密后的密文：" + encryptedData);
    }

    private static PublicKey generatePublicKey(String publicKeyStr) {
        try {
            byte[] keyBytes = java.util.Base64.getDecoder().decode(publicKeyStr);
            X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            return keyFactory.generatePublic(spec);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static String convertToJson(DataObject dataObject) {
        // 这里可以使用 JSON 库（如 Jackson、Gson 等）进行实际的 JSON 转换
        return "{\"active_time_id\":" + dataObject.getActive_time_id() + ",\"contact_phone\":\"" + dataObject.getContact_phone() + "\",\"_time\":" + dataObject.getTime() + "}";
    }

    private static List<String> encryptData(String data, PublicKey publicKey) {
        List<String> encryptedChunks = new ArrayList<>();
        try {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] dataBytes = data.getBytes();
            for (int i = 0; i < dataBytes.length; i += 245) {
                int endIndex = Math.min(i + 245, dataBytes.length);
                byte[] chunk = new byte[endIndex - i];
                System.arraycopy(dataBytes, i, chunk, 0, chunk.length);
                byte[] encryptedChunk = cipher.doFinal(chunk);
                encryptedChunks.add(java.util.Base64.getEncoder().encodeToString(encryptedChunk));
            }
            return encryptedChunks;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

class DataObject {
    private int active_time_id;
    private String contact_phone;
    private long _time;

    public DataObject(int active_time_id, String contact_phone) {
        this.active_time_id = active_time_id;
        this.contact_phone = contact_phone;
    }

    public int getActive_time_id() {
        return active_time_id;
    }

    public void setActive_time_id(int active_time_id) {
        this.active_time_id = active_time_id;
    }

    public String getContact_phone() {
        return contact_phone;
    }

    public void setContact_phone(String contact_phone) {
        this.contact_phone = contact_phone;
    }

    public long getTime() {
        return _time;
    }

    public void setTime(long time) {
        _time = time;
    }
}